package com.hc;

import com.hc.dao.UserDao;
import com.hc.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanApplication {
    /**
     * IOC原理
     * 1.Spring容器启动时,加载核心配置文件
     * 2.读取并解析bean标签
     * 3.根据id,class属性反射创建对象并存入Map中
     * 4.需要对象时根据id获取
     */
    @Test
    public void f1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        UserDao userDao = (UserDao) context.getBean("userDao");//根据id获取
        userDao.save();
        UserDao userDao1 = (UserDao) context.getBean("user_dao");//根据别名获取
        System.out.println(userDao==userDao1);
        UserDao userDao2 = context.getBean(UserDao.class);//根据类型获取
        System.out.println(userDao1 == userDao2);
        UserService userService = (UserService) context.getBean("userService");
        userService.save();
    }
}
